package com.indusfo.spc.socket;




import com.indusfo.spc.service.BoardService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.annotation.Resource;
import javax.websocket.Session;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;


/**
 * 定时事务
 *
 * @author TYw
 * @descroption
 * @date 2019/9/27 14:54
 */

public class PrintPlatoJob implements Job {

	@Resource
    BoardService boardService;

//	private static String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

	private static HashMap<Session, String> timeMap = new HashMap<>();

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
		JobDataMap map = jobExecutionContext.getJobDetail().getJobDataMap();
		try {
			//获取session
			Session session = (Session) map.get("session");
            Integer fixedDeteId = (Integer)map.get("fixedDeteId");

            //获取时间节点
			String time = timeMap.get(session);
			if (time == null) {
				time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
			//存放时间节点
			time = sdf.format(new Date());
			timeMap.put(session, time);
//            JSONObject fixedDetectionResult = boardService.queryFixedDetectionResult(fixedDeteId);
//            String json = new Gson().toJson(fixedDetectionResult);
			String json = "aa";
			session.getBasicRemote().sendText(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
//		catch (ParseException e) {
//			e.printStackTrace();
//		}

	}
}
